package com.newly.driver.sdk.device.video.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.crypto.SecureUtil;
import com.newly.driver.sdk.device.video.cloud.device.QueryDeviceResp;
import com.newly.driver.sdk.device.video.constants.DeviceConstant;
import com.newly.driver.sdk.device.video.service.api.IDevice;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @author gjm guan
 * @date 2021/10/26 16:27
 * @desc 云平台基类
 */
@Component
public abstract class AbstractCloud {

    @Autowired
    private IDevice device;

    /**
     * 获取设备状态，公共方法
     *
     * @param deviceId 设备id
     * @return DeviceConstant.DeviceState.enum
     */
    protected DeviceConstant.DeviceState queryDeviceState(String deviceId) {
        QueryDeviceResp queryDeviceResp = device.queryDevice(deviceId);
        if (!ObjectUtil.isEmpty(queryDeviceResp) &&
                !ObjectUtil.isEmpty(queryDeviceResp.getPayloads())) {
            int status = queryDeviceResp.getPayloads().get(0).getStatus();
            return DeviceConstant.DeviceState.getInstance(status);
        }
        return DeviceConstant.DeviceState.OFFLINE;
    }

    /**
     * 获取设备类型
     *
     * @param deviceId 设备id
     * @return DeviceConstant.DeviceType.enum
     */
    protected DeviceConstant.DeviceType queryDeviceType(String deviceId) {
        QueryDeviceResp queryDeviceResp = device.queryDevice(deviceId);
        if (!ObjectUtil.isEmpty(queryDeviceResp) &&
                !ObjectUtil.isEmpty(queryDeviceResp.getPayloads())) {
            int status = queryDeviceResp.getPayloads().get(0).getDeviceType();
            return DeviceConstant.DeviceType.getInstance(status);
        }
        return null;
    }

    /**
     * 获取录制id
     *
     * @param deviceId  设备id
     * @param channelId 通道id
     * @return 录制id
     */
    protected String getRecordId(String deviceId, String channelId) {
        return SecureUtil.md5(String.format("%s%s", deviceId, channelId))
                .replace("-", "").substring(10);
    }
}
